/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.front.web.pay;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.thinkgem.jeesite.common.utils.CommonParameter;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.config.service.pay.ConfigPayManageService;
import com.thinkgem.jeesite.modules.pay.entity.log.EcPayLog;
import com.thinkgem.jeesite.modules.pay.entity.upop.EcOrgUpop;
import com.thinkgem.jeesite.modules.pay.sdk.AcpService;
import com.thinkgem.jeesite.modules.pay.sdk.LogUtil;
import com.thinkgem.jeesite.modules.pay.sdk.SDKConfig;
import com.thinkgem.jeesite.modules.pay.service.upop.EcOrgUpopService;
import com.thinkgem.jeesite.modules.pay.util.PayUtil;
import com.thinkgem.jeesite.modules.pay.util.UPOPUtil.UPOPInstall;
import com.thinkgem.jeesite.modules.shop.entity.order.ShopOrder;
import com.thinkgem.jeesite.modules.shop.service.order.ShopOrderService;

/**
 * 支付Controller（FRONT）
 * 
 * @author Egan
 * @version 2016-12-15
 */
@Controller
@RequestMapping(value = "${frontPath}/front/pay")
public class PayController extends BaseController {
	
	@Autowired
	ConfigPayManageService configPayManageService;
	
	@Autowired
	ShopOrderService orderService;
	
	@Autowired 
	EcOrgUpopService ecOrgUpopService;
	@RequestMapping(value = "showBank")
	public String list(ShopOrder order, HttpServletRequest request, HttpServletResponse resp, Model model) {
		order = orderService.get(order);
		model.addAttribute("path", CommonParameter.uploadPath);
		model.addAttribute("order", order);
		return "modules/shop/order/make_order_success";
	}

	@RequestMapping(value = "showBank2")
	public String showBank2(ShopOrder order, HttpServletRequest request, HttpServletResponse resp, Model model) {
		order = orderService.get(order);
		model.addAttribute("path", CommonParameter.uploadPath);
		model.addAttribute("order", order);
		return "modules/shop/order/immediate_payment";
	}

	
	@RequestMapping(value = "forPay")
	public void forPay(HttpServletRequest req, HttpServletResponse resp, Model model) {
		try {
			//前台页面传过来的
			String configPayId = req.getParameter("configPayId");
			String coNum = req.getParameter("coNum");
			String txnAmt = req.getParameter("txnAmt");
			EcOrgUpop upop = ecOrgUpopService.get(configPayId);
			
			EcPayLog log = new EcPayLog();
			log.setMerChantid(upop.getMerchantId());
			log.setAmount(new BigDecimal(txnAmt));
			log.setCoNum(coNum);
			Map<String, String> requestData = UPOPInstall.installParamsForPay(log);
			//////////////////////////////////////////////////
			// 报文中特殊用法请查看 PCwap网关跳转支付特殊用法.txt
			//////////////////////////////////////////////////
			/**请求参数设置完毕，以下对请求参数进行签名并生成html表单，将表单写入浏览器跳转打开银联页面**/
			//报文中certId,signature的值是在signData方法中获取并自动赋值的，只要证书配置正确即可。
			Map<String, String> submitFromData = AcpService.sign(requestData,PayUtil.encoding_UTF8);  
			//获取请求银联的前台地址：对应属性文件acp_sdk.properties文件中的acpsdk.frontTransUrl
			String requestFrontUrl = SDKConfig.getConfig().getFrontRequestUrl();  
			//生成自动跳转的Html表单
			String html = AcpService.createAutoFormHtml(requestFrontUrl, submitFromData,PayUtil.encoding_UTF8);   
			
			LogUtil.writeLog("打印请求HTML，此为请求报文，为联调排查问题的依据："+html);
			//将生成的html写到浏览器中完成自动跳转打开银联支付页面；这里调用signData之后，将html写到浏览器跳转到银联页面之前均不能对html中的表单项的名称和值进行修改，如果修改会导致验签不通过
			resp.getWriter().write(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}